//
//  DataBarChartView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/15.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa

open class ActivityBarChartView: View {
    
    public var barClolr:UIColor = .gray
    public var barHighLichtColor:UIColor = .lightGray
    public var barWidth:Double = 0.4
    public var dataTextColor:UIColor = .white {
        didSet{
            self.dataLabel.textColor = dataTextColor
        }
    }
    public var unitTextColor:UIColor = .white {
        didSet{
            self.unitLabel.textColor = unitTextColor
        }
    }
    public var timeTextColor:UIColor = .white {
        didSet{
            self.timeLabel.textColor = timeTextColor
        }
    }
    
    open override func makeUI() {
        super.makeUI()
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .firstBaseline
        dataStack.contentMode = .center
        dataStack.addArrangedSubview(dataLabel)
        dataStack.addArrangedSubview(unitLabel)
        

        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)
        
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        let stackView = StackView()
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fillEqually
        stackView.backgroundColor = UIColor.background
        stackView.addArrangedSubview(self.distanceView)
        stackView.addArrangedSubview(self.kcalView)
        stackView.addArrangedSubview(self.timeView)
        
        self.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(24)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(40)
        }
        
        self.addSubview(barChartView)
        barChartView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(120)
            make.top.equalTo(stackView.snp.bottom).offset(6)
        }
    }
    
    public func bind(by viewModel:BaseDataChartViewModel) {
        let input = BaseDataChartViewModel.Input(selectIndex: self.barChartView.selectIndex)
        let output = viewModel.transform(input: input)
        
//        let disposeBag = DisposeBag()
//        output.dataString.drive(self.dataLabel.rx.text).disposed(by: rx.disposeBag)
//        output.timeString.drive(self.timeLabel.rx.text).disposed(by: rx.disposeBag)
        
        self.unitLabel.text = viewModel.getUnitString()
        
        self.barChartView.setBarCount(viewModel.getBarCount())
        self.barChartView.setXAxis(valueFormatter: viewModel.getXAxisValueFotmatter())
        self.barChartView.setYMax(value: viewModel.getBarMaxValue())
        
//        viewModel.dataSource.map {
//
//            let maxData = max($0, viewModel.getBarMinValue())
//            return maxData
//
//        }
        
        self.barChartView.setData(viewModel.dataSource,
                                  barColor: self.barClolr,
                                  barHighLichtColor: self.barHighLichtColor,
                                  barWidth: self.barWidth)
        
    }
    
    public func setChartConfig(_ config:DFBarChartViewConfig){
        self.barChartView.setConfig(config)
    }
    
    public func slider(_ point:CGPoint){
        self.barChartView.setTouchLocation(point)
    }
    
    public func clearHighlight() {

        self.barChartView.highlightValue(nil)
        
    }
    
    //MARK: - lazy load -
    lazy var barChartView: DFBarChartView = {
        
        let barChartView = DFBarChartView(frame:.zero)
        
        // 设置Y轴
        barChartView.leftAxis.labelTextColor = UIColor.assist_D0D0D0
        barChartView.leftAxis.axisLineColor = .clear
        barChartView.leftAxis.enabled = true
        barChartView.leftAxis.drawGridLinesEnabled = true
        barChartView.leftAxis.gridLineDashLengths = [3.0, 3.0] //线段类型
        barChartView.leftAxis.gridColor = UIColor.assist_EEEEEE //网格线颜色
        barChartView.leftAxis.gridAntialiasEnabled = true//开启抗锯齿
        barChartView.leftAxis.labelCount = 4
        barChartView.leftAxis.forceLabelsEnabled = true
        
        
        
        return barChartView
    }()
    
    lazy var dataLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 40)
        return label
    }()
    
    lazy var timeLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        return label
    }()
    
    lazy var unitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        return label
    }()
    
    lazy var distanceView: ActivityCurrentTotalItem = {
        let view = ActivityCurrentTotalItem()
        view.unitLabel.text = "unit_km".language
        view.imageView.image = UIImage.init(named: "ic_home_today_steps_distance")
        return view
    }()

    lazy var kcalView: ActivityCurrentTotalItem = {
        let view = ActivityCurrentTotalItem()
        view.unitLabel.text = "unit_calorie".language
        view.imageView.image = UIImage.init(named: "ic_home_today_steps_calorie")
        return view
    }()

    lazy var timeView: ActivityCurrentTotalItem = {
        let view = ActivityCurrentTotalItem()
        view.unitLabel.text = "unit_minute".language
        view.imageView.image = UIImage.init(named: "ic_home_today_steps_duration")
        return view
    }()
    
}


